# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=gobject-introspection
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-runtime")
pkgver=1.56.1
pkgrel=1
arch=('any')
url="https://live.gnome.org/GObjectIntrospection"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-mako"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
# Building this in debug invokes pdb when things go wrong.
# options=('!strip' 'debug')
source=(https://download.gnome.org/sources/gobject-introspection/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        0001-Revert-Windows-port-Work-arount-MSYS-weirdness-where.patch
        0021-tests-no-undefined.patch
        0022-change-pkg-config-invocations.mingw.patch
        0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
        0025-more-tolerant-rmtreeing.patch
        0026-giscanner-assertions-and-waits.patch
        0027-wait-for-xml-parse-too.patch
        0055-fix-python-detection.patch
        py3-fixes.patch
        pyscript2exe.py)

sha256sums=('5b2875ccff99ff7baab63a34b67f8c920def240e178ff50add809e267d9ea24b'
            'f84df99e1ce5a3a0b640ef172de068958eb0b3d0be43a454dd86e6ccb8cfc386'
            '663bb39764058b1de1591765f9f465dd69eeae16fcf56b82a3692560849dacae'
            'bb5f48047613216a6c3c652e0ef65c099475ffdddbea4a835b5a51279d7755ac'
            'fd461c3458aa174126b287c659ba3ce7c9a5402c844562022293790988783d30'
            'f7fa69b6af7571371b1e162cacac05fc2a2877d057dd61649948d973b74a9e8d'
            '19ca830262339c4ac9f21bfb8d669ee8e126a949a4237d55656e00c5ae81c451'
            '3f38bdd0dd43d9cf626cbca7c2abd22a7ed0213e6756252c6d467d470d9c5948'
            'f61d099aa7cd37c437f01d98bb95c57c66f07c78028675ae19fac87f943d189d'
            'c91a0604e4cbc483099da94ab61a7dc32cb7337f03c5561ac5950c12e6f3d9af'
            'f68b24932b3365c4098c04eeaeaf87275ceec29694b3f0597c431bbcf4f913a3')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-Revert-Windows-port-Work-arount-MSYS-weirdness-where.patch
  patch -p1 -i "${srcdir}"/0021-tests-no-undefined.patch
  patch -p1 -i "${srcdir}"/0022-change-pkg-config-invocations.mingw.patch
  # Source for this patch is:
  # https://bazaar.launchpad.net/~lrn1986/stupidbuild/trunk/view/head:/src/mingw/gobject-introspection-1.0-1.44.0-2/patches/0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
  patch -p1 -i "${srcdir}"/0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
  patch -p1 -i "${srcdir}"/0025-more-tolerant-rmtreeing.patch
  patch -p1 -i "${srcdir}"/0026-giscanner-assertions-and-waits.patch
  patch -p1 -i "${srcdir}"/0027-wait-for-xml-parse-too.patch
  patch -p1 -i "${srcdir}"/0055-fix-python-detection.patch
  patch -p1 -i "${srcdir}"/py3-fixes.patch

  autoreconf -fi
}

build() {
  export PYTHON=${MINGW_PREFIX}/bin/python3
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-silent-rules

  make
}

package_gobject-introspection() {
  pkgdesc="Introspection system for GObject-based libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-pkg-config"
           "${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-mako")
  options=('!emptydirs')

  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  make -j1 DESTDIR="${pkgdir}" uninstall-libLTLIBRARIES uninstall-typelibsDATA

  # I've spent too long debugging gobject-introspection and it's too painful each
  # time. Therefore, I've taken the decision to copy the .pyd also as a _d.pyd
  # in-case I happen to be investigating it with a debug version of Python which
  # only loads _d.pyd extensions.
  # You will still have to rebuild this package after having built and installed
  # the debug mingw-w64-python2 of course .. still one less thing to forget to
  # remember.
  # There will be a way to communicate the nature of the Python through to Autoconf
  # and that would be better, but this will have to do for now.
  # (see:
  #  @OS_WIN32_TRUE@	-shrext ".pyd"
  #  in gobject-introspection/Makefile.in)

  # Disabled since the Python 3 switch
  #~ PYDS=$(find "${pkgdir}" -name "*.pyd")
  #~ for PYD in "${PYDS}"; do
    #~ pushd $(dirname "${PYD}")
      #~ cp $(basename "${PYD}") $(basename "${PYD}" .pyd)_d.pyd
    #~ popd
  #~ done

  for name in g-ir-scanner g-ir-doc-tool g-ir-annotation-tool; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/${name}"
  done
}

package_gobject-introspection-runtime() {
  pkgdesc="Introspection system for GObject-based libraries - runtime files (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2")

  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install-libLTLIBRARIES install-typelibsDATA
}

# Wrappers
package_mingw-w64-i686-gobject-introspection()
{
  package_gobject-introspection
}

package_mingw-w64-i686-gobject-introspection-runtime()
{
  package_gobject-introspection-runtime
}

package_mingw-w64-x86_64-gobject-introspection()
{
  package_gobject-introspection
}

package_mingw-w64-x86_64-gobject-introspection-runtime()
{
  package_gobject-introspection-runtime
}
